# 代码回顾
# 元素定位的常见方式
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

# 通过id定位

# class BrowserOperate:
#
#     def __init__(self):
#         self.driver=webdriver.Chrome()
#         self.driver.get('https://www.baidu.com')
#
#     def ID(self):
#         a=self.driver.find_element(By.ID,'KW')
#         print(a.get_attribute('outerHTML'))
#
#     def classname(self):
#         b=self.driver.find_element(By.CLASS_NAME,'s_ipt')
#         print(b.get_attribute('outerHTML'))
#
#     def linktext(self):
#         c = self.driver.find_element(By.LINK_TEXT, '新闻')
#         print(c.get_attribute('outerHTML'))
#
#     def partial(self):
#         d = self.driver.find_element(By.PARTIAL_LINK_TEXT, 'hao')
#         print(d.get_attribute('outerHTML'))
#
#     def name(self):
#         e = self.driver.find_element(By.NAME, 'wd')
#         print(e.get_attribute('outerHTML'))
#
#     def tagname(self):
#         f = self.driver.find_element(By.TAG_NAME, 'input')
#         print(f.get_attribute('outerHTML'))
#         print('*'*100)
#         g= self.driver.find_elements(By.TAG_NAME, 'input')
#         for i in g:
#             print(i.get_attribute('outerHTML'))
#
#     def quit(self,seconds=5):
#         time.sleep(seconds)
#         self.driver.quit()
#

#
# if __name__=='__main__':
#     bo = BrowserOperate()
#     bo.ID()
#     bo.classname()
#     bo.linktext()
#     bo.partial()
#     bo.name()
#     bo.tagname()
#     bo.quit()



# xpath定位的方法
# 1标签和元素组合的方法

driver=webdriver.Chrome()
driver.get('https://www.baidu.com')


kw=driver.find_element(By.XPATH,'//input[@id="kw"]')
print(kw.get_attribute('outerHTML'))

wd=driver.find_element(By.XPATH,'//input[@name="wd"]')
print(wd.get_attribute('outerHTML'))


inputs=driver.find_elements(By.XPATH,'//input')
for i in inputs:
    print(i.get_attribute('outerHTML'))


# 层级
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

driver=webdriver.Chrome()
driver.get('https://www.baidu.com')

a=driver.find_element(By.XPATH,'//form/input')
print(a.get_attribute('outerHTML'))

b=driver.find_element(By.XPATH,'//form/span/input')
print(b.get_attribute('outerHTML'))

# 索引
driver=webdriver.Chrome()
driver.get('https://www.baidu.com')

a=driver.find_element(By.XPATH,'//form/input')
print(a.get_attribute('outerHTML'))

b=driver.find_element(By.XPATH,'//form/span/input')
print(b.get_attribute('outerHTML'))

# 逻辑
driver=webdriver.Chrome()
driver.get('https://www.baidu.com')

a=driver.find_element(By.XPATH,'//input[@id="kw" and @name="wd"]')
print(a.get_attribute('outerHTML'))

# 模糊
a=driver.find_element(By.XPATH,'//a[contains(text(),"hao")]')
print(a.get_attribute('outerHTML'))
#
b=driver.find_element(By.XPATH,'//*[contains(@id,"k")]')
print(b.get_attribute('outerHTML'))











# 2 练习下面网址的操作
# 网址：ecshop演示站
# 1. 首页搜索操作
# 2. 注册操作
# 3. 登录操作
# class BrowserOper:
#     def __init__(self,msg):
#         self.driver=webdriver.Chrome()
#         self.driver.get('https://ecshop.test2.shopex123.com')
#         self.msg=msg
#
#
#     def search(self):
#         self.driver.find_element(By.ID,'keyword').send_keys(f'{self.msg}')
#         self.driver.find_element(By.NAME,'imageField').click()
#
#     def login(self):
#         self.driver.find_element(By.PARTIAL_LINK_TEXT,'登录').click()
#
#
#     def resg(self):
#         self.driver.find_element(By.PARTIAL_LINK_TEXT, '注册').click()
#
#
#     def quit(self):
#         time.sleep(3)
#         self.driver.quit()
#
# bo = BrowserOper('欧莱雅')
# # bo.search()
# # bo.login()
# bo.resg()
# bo.quit()

class BrowserOper1:
    def __init__(self,msg):
        self.driver=webdriver.Chrome()
        self.driver.get('https://ecshop.test2.shopex123.com')
        self.msg=msg


    def search(self):
        self.driver.find_element(By.XPATH,'//input[1]').send_keys(f'{self.msg}')
        self.driver.find_element(By.XPATH,'//input[contains(@type,"mit")]').click()

    def login(self):
        self.driver.find_element(By.PARTIAL_LINK_TEXT,'登录').click()


    def resg(self):
        self.driver.find_element(By.PARTIAL_LINK_TEXT, '注册').click()


    def quit(self):
        time.sleep(3)
        self.driver.quit()

bo = BrowserOper1('欧莱雅')
bo.search()
# # bo.login()
# bo.resg()
bo.quit()





